const express = require("express");
const app= express();

app.get("/server",(request,response)=>{
    
    response.setHeader("Access-Control-Allow-Origin","*");
    response.send("Hello Ajax -2");
});
app.all("/post-server",(request,response)=>{
    
    response.setHeader("Access-Control-Allow-Origin","*");
    response.setHeader("Access-Control-Allow-Headers","*");
    response.send("Hello Ajax Post");
});
app.all("/json-server",(request,response)=>{
    response.setHeader("Access-Control-Allow-Origin","*");
    response.setHeader("Access-Control-Allow-Headers","*");

    const data = {
        name:"wangteng",
        age:22
    }
    let str = JSON.stringify(data);
    response.send(str);
});
//IE浏览器缓存问题
app.get("/IE-server",(request,response)=>{
    
    response.setHeader("Access-Control-Allow-Origin","*");
    response.send("Hello IE -2");
});
//延时响应
app.get("/delay",(request,response)=>{
    
    response.setHeader("Access-Control-Allow-Origin","*");
    setTimeout(function(){
         response.send("这是延时响应！");
    },3000)
   
});
//axios服务
app.all("/axios-server",(request,response)=>{
    response.setHeader("Access-Control-Allow-Origin","*");
    response.setHeader("Access-Control-Allow-Headers","*");

    const data = {
        name:"尚硅谷",
    };
    response.send(JSON.stringify(data));
});
//fetch函数服务
app.all("/fetch-server",(request,response)=>{
    response.setHeader("Access-Control-Allow-Origin","*");
    response.setHeader("Access-Control-Allow-Headers","*");

    const data = {
        name:"尚硅谷",
    };
    response.send(JSON.stringify(data));
});
app.listen(8000,()=>{
    console.log("服务已经启动，8000端口监听中......");
});